package com.hand.rch25287.zuoye003.service;

import com.hand.rch25287.zuoye003.mapper.UserMapper;
import com.hand.rch25287.zuoye003.entity.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author 25287
 * @create 2019 - 07 16 20:20
 */
public class UserService {
    private static ApplicationContext ctx;
    private static UserMapper userMapper;
    static {
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        userMapper = (UserMapper) ctx.getBean(UserMapper.class);
    }

    public int insertUser(User user){
//        try {
//            if (userNumberIsExist(user.getUserNumber())) {
//                System.out.println("userNumber: " +
//                        user.getUserNumber() +
//                        " 存在, 请修改后再添加");
//                return 0;
//            }
//
//            userMapper.insertUser(user);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
        userMapper.insertUser(user);
        int id = user.getUserId().intValue();
        return id;
    }

    public int updateUser(User user){
        User userResult = null;
        try {
            if (!userIdIsExist(user.getUserId().longValue())) {
                System.out.println("userId: " +
                        user.getUserId() +
                        " 不存在, 请修改后再更新");
                return -1;
            }
            userResult = getOneUserByName(user.getUserNumber());
            if (userResult != null && !userResult.getUserId().equals(user.getUserId())) {
                System.out.println("userNumber: " +
                        user.getUserNumber() +
                        " 存在, 请修改后再更新");
                return 0;
            }

            userMapper.updateUser(user);
        }catch (Exception e){
            e.printStackTrace();
        }

        int id = user.getUserId().intValue();
        return id;
    }

    public int deleteUser(long userId){
        int count = 0;
        try {
            count = userMapper.deleteUser(userId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    public User getOneUserById(long id){
        User user = null;

        try {
            user = userMapper.getOneUserById(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }

    static public User getOneUserByName(String userNumber){
        User user = null;

        try {
            user = userMapper.getOneUserByNumber(userNumber);
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }

    static public boolean userNumberIsExist(String number){
        User user = null;
        try {
            user = userMapper.getOneUserByNumber(number);
        }catch (Exception e){
            e.printStackTrace();
        }
        return user!=null;
    }
    static public boolean userIdIsExist(long userId){
        User user = null;
        try {
            user = userMapper.getOneUserById(userId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return user!=null;
    }
}
